﻿using Janus.BaseEntitys;
using Janus.Mes.DTO;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace Janus.Mes.IDal
{
    public interface IBaseDal<POCO, DTO> where POCO : Entity where DTO : BaseDTO
    {
        /// <summary>
        /// 指定条件记录是否存在
        /// </summary>
        /// <param name="predicate">lamda条件</param>
        /// <returns></returns>
        bool IsExists(string predicate);

        /// <summary>
        /// 指定条件记录是否存在
        /// </summary>
        /// <typeparam name="POCO"></typeparam>
        /// <param name="predicate"></param>
        /// <returns></returns>
        bool IsExists<POCO>(Expression<Func<POCO, bool>> predicate) where POCO : Entity;

        /// <summary>
        /// 指定条件记录是否存在
        /// </summary>
        /// <typeparam name="POCO"></typeparam>
        /// <param name="predicate"></param>
        /// <returns></returns>
        bool IsExists<POCO>(Querier predicate) where POCO : Entity;

        #region 查询
        /// <summary>
        /// 根据指定条件查询并返回指定字段
        /// </summary>
        /// <typeparam name="POCO"></typeparam>
        /// <param name="field">返回字段</param>
        /// <param name="predicate">指定条件</param>
        /// <returns></returns>
        object FindScalar<POCO>(string field, string predicate) where POCO : Entity;

        /// <summary>
        /// 根据指定条件查询并返回指定字段
        /// </summary>
        /// <typeparam name="POCO"></typeparam>
        /// <param name="field">返回字段</param>
        /// <param name="predicate">指定条件</param>
        /// <returns></returns>
        object FindScalar<POCO>(string field, Querier predicate) where POCO : Entity;

        /// <summary>
        /// 根据指定条件查询并返回指定字段
        /// </summary>
        /// <typeparam name="POCO"></typeparam>
        /// <param name="field"></param>
        /// <param name="predicate"></param>
        /// <returns></returns>
        object FindScalar<POCO>(Expression<Func<POCO, string>> field, Expression<Func<POCO, bool>> predicate) where POCO : Entity;

        /// <summary>
        /// 根据主键获取单个对象
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        DTO FindOne<POCO, DTO>(params object[] entity) where POCO : Entity where DTO : BaseDTO;

        /// <summary>
        /// 根据条件获取一个对象
        /// </summary>
        /// <typeparam name="POCO"></typeparam>
        /// <param name="predicate">SQL 条件</param>
        /// <returns></returns>
        DTO FindOne<POCO, DTO>(string predicate) where POCO : Entity where DTO : BaseDTO;

        /// <summary>
        /// 根据条件获取一个对象
        /// </summary>
        /// <typeparam name="POCO"></typeparam>
        /// <param name="predicate">lamda条件表达式</param>
        /// <returns></returns>
        DTO FindOne<POCO, DTO>(Querier predicate) where POCO : Entity where DTO : BaseDTO;

        /// <summary>
        /// 根据条件获取一个对象
        /// </summary>
        /// <typeparam name="POCO"></typeparam>
        /// <param name="predicate"></param>
        /// <returns></returns>
        DTO FindOne<POCO, DTO>(Expression<Func<POCO, bool>> predicate) where POCO : Entity where DTO : BaseDTO;


        /// <summary>
        /// 根据条件获取列表数据
        /// </summary>
        /// <typeparam name="POCO"></typeparam>
        /// <param name="predicate">lamda条件表达式</param>
        /// <param name="orderBy">排序</param>
        /// <returns></returns>
        IList<DTO> FindAll<POCO, DTO>(string predicate, string orderBy = null) where POCO : Entity where DTO : BaseDTO;

        /// <summary>
        /// 根据条件获取列表数据
        /// </summary>
        /// <typeparam name="POCO"></typeparam>
        /// <param name="predicate">lamda条件表达式</param>
        /// <param name="orderBy">排序</param>
        /// <returns></returns>
        IList<DTO> FindAll<POCO, DTO>(Querier predicate) where POCO : Entity where DTO : BaseDTO;

        /// <summary>
        /// 根据条件获取列表数据
        /// </summary>
        /// <typeparam name="POCO"></typeparam>
        /// <param name="predicate"></param>
        /// <returns></returns>
        IList<DTO> FindAll<POCO, DTO>(Expression<Func<POCO, bool>> predicate, string orderBy = null) where POCO : Entity where DTO : BaseDTO;

        /// <summary>
        /// 返回数据列表
        /// </summary>
        /// <param name="searchEntity"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        ResponseResult<IList<DTO>> FindAll<POCO, DTO>(SearchEntity<POCO> searchEntity) where POCO : Entity, new() where DTO : BaseDTO, new();

        /// <summary>
        /// 使用存储过程分页 性能最好
        /// </summary>
        /// <param name="searchEntity"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        ResponseResult<IList<DTO>> FindAllByPro<POCO, DTO>(PageCriteria pageCriteria) where POCO : Entity, new() where DTO : BaseDTO, new();

        #endregion

        #region 新增
        /// <summary>
        /// 添加一条记录
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        bool Add<DTO, POCO>(DTO t) where DTO : BaseDTO where POCO : Entity;

        /// <summary>
        /// 添加多条记录，返回行数
        /// </summary>
        /// <param name="ts"></param>
        /// <returns></returns>
        bool Add<DTO, POCO>(List<DTO> ts) where DTO : BaseDTO where POCO : Entity;

        #endregion

        #region 更新
        /// <summary>
        /// 更新对象（根据主键）,不存在则新增
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        int Save<DTO, POCO>(DTO t) where DTO : BaseDTO where POCO : Entity;

        /// <summary>
        /// 批量更新对象
        /// </summary>
        /// <param name="entitys"></param>
        /// <returns></returns>
        bool Save<DTO, POCO>(List<DTO> entitys) where DTO : BaseDTO where POCO : Entity;

        /// <summary>
        /// 根据主键编辑对象
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        int Update<DTO, POCO>(DTO t) where DTO : BaseDTO where POCO : Entity;

        /// <summary>
        /// 根据主健更新列表
        /// </summary>
        /// <param name="entitys"></param>
        /// <returns></returns>
        bool Update<DTO, POCO>(List<DTO> entitys) where DTO : BaseDTO where POCO : Entity;

        /// <summary>
        /// 更新部分类
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="conditions"></param>
        /// <returns></returns>
        int UpdateSetion(DynamicEntity entity, Conditioner conditions);

        /// <summary>
        /// 更新部分类
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="conditions"></param>
        /// <returns></returns>
        int UpdateSetion<POCO>(DynamicEntity entity, Querier conditions) where POCO : Entity;

        /// <summary>
        /// 更新部分类
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="conditions"></param>
        /// <returns></returns>        
        int UpdateSetion<DTO, POCO>(DTO entity, Conditioner conditions) where DTO : BaseDTO where POCO : Entity;

        /// <summary>
        /// 更新部分类
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="conditions"></param>
        /// <returns></returns>   
        int UpdateSetion<DTO, POCO>(DTO entity, Querier conditions) where DTO : BaseDTO where POCO : Entity;

        /// <summary>
        /// 更新部份类
        /// </summary>
        /// <param name="updater"></param>
        /// <param name="predicate"></param>
        /// <returns></returns>
        int UpdateSetion(Hashtable updater, string predicate);

        /// <summary>
        /// 更新部份类
        /// </summary>
        /// <param name="updater"></param>
        /// <param name="predicate"></param>
        /// <returns></returns>
        int UpdateSetion<POCO>(Hashtable updater, Querier predicate) where POCO : Entity;

        /// <summary>
        /// 更新部份类
        /// </summary>
        /// <typeparam name="POCO"></typeparam>
        /// <param name="updater"></param>
        /// <param name="predicate"></param>
        /// <returns></returns>
        int UpdateSetion<POCO>(Expression<Func<POCO, POCO>> updater, Expression<Func<POCO, bool>> predicate) where POCO : Entity;

        /// <summary>
        /// 更新停用启用状态
        /// </summary>
        /// <typeparam name="POCO"></typeparam>
        /// <param name="queriers"></param>
        /// <returns></returns>
        bool UpdateIsEnable<POCO>(Querier queriers) where POCO : Entity;
        #endregion

            #region 删除
        /// <summary>
        /// 根据主键删除对象
        /// </summary>
        /// <param name="keyVal">主健值</param>
        /// <returns></returns>
        bool Delete(params object[] entity);

        /// <summary>
        /// 根据复杂条件删除数据
        /// </summary>
        /// <param name="conditions"></param>
        /// <returns></returns>
        bool Delete(Conditioner conditions);

        /// <summary>
        /// 根据复杂条件删除数据
        /// </summary>
        /// <param name="predicate">lamda条件</param>
        /// <returns></returns>
        bool Delete(string predicate);

        // <summary>
        /// 根据复杂条件删除数据
        /// </summary>
        /// <param name="predicate">lamda条件</param>
        /// <returns></returns>
        bool Delete<POCO>(Querier predicate) where POCO : Entity;

        /// <summary>
        /// Lamda表达式删除
        /// </summary>
        /// <typeparam name="POCO"></typeparam>
        /// <param name="predicate"></param>
        /// <returns></returns>
        bool Delete<POCO>(Expression<Func<POCO, bool>> predicate) where POCO : Entity;
        /// <summary>
        /// 清空表
        /// </summary>
        /// <returns></returns>
        //int DeleteAll();

        #endregion

        #region 批量操作
        /// <summary>
        /// 批量插入
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="models"></param>
        /// <returns></returns>
        bool BatchInset<DTO,POCO>(List<DTO> models) where DTO : BaseDTO where POCO : Entity;

        /// <summary>
        /// 批量更新(需提供RowState状态)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="models"></param>
        /// <returns></returns>
        bool BatchUpdate<DTO, POCO>(List<DTO> models) where DTO : BaseDTO where POCO : Entity;

        /// <summary>
        /// 批量更新或插入(需提供RowState状态)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="models"></param>
        /// <returns></returns>
        bool BatchSave<DTO, POCO>(List<DTO> models) where DTO : BaseDTO where POCO : Entity;
        #endregion
    }
}
